﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace SPN.Common
{
    public class Helper
    {
        #region ConstProperties
        public const string _Const_NewsletterListName = "SPN - Newsletter List";
        public const string _Const_SubscriberListName = "SPN - Subscriber List";
        //==========================================================================================================================>>
        public const string _SPN_ConfirmationUrl = "SPN_ConfirmationUrl";
        public const string _SPN_SubscribeEmailFrom = "SPN_SubscribeEmailFrom";
        public const string _SPN_SubscribeEmailSubject = "SPN_SubscribeEmailSubject";
        public const string _SPN_SubscribeEmailContent = "SPN_SubscribeEmailContent";
        public const string _SPN_SubscribeEmailLinkText = "SPN_SubscribeEmailLinkText";
        public const string _SPN_UnSubscribeEmailFrom = "SPN_UnSubscribeEmailFrom";
        public const string _SPN_UnSubscribeEmailSubject = "SPN_UnSubscribeEmailSubject";
        public const string _SPN_UnSubscribeEmailContent = "SPN_UnSubscribeEmailContent";
        public const string _SPN_UnSubscribeEmailLinkText = "SPN_UnSubscribeEmailLinkText";
        #endregion
        //==========================================================================================================================>>
        public static List<Entities.Newsletter> GetNewsletterItems(SPWeb _Web)
        {
            List<Entities.Newsletter> _NewsletterItems = new List<Entities.Newsletter>();
            //=====================================================================>>
            if (IsListValid(_Web, Common.Helper._Const_NewsletterListName))
            {
                SPList _List = _Web.Lists[Common.Helper._Const_NewsletterListName];
                SPQuery _Query = new SPQuery();
                _Query.Query = "<Where><Eq><FieldRef Name=\"ContentType\" /><Value Type=\"Computed\">SPN - Newsletter</Value></Eq></Where>";
                SPListItemCollection _ListItems = _List.GetItems(_Query);
                for (int i = 0; i < _ListItems.Count; i++)
                {
                    SPListItem _ListItem = _ListItems[i];
                    Entities.Newsletter _NewsletterItem = new Entities.Newsletter();
                    _NewsletterItem.Id = _ListItem.ID;
                    _NewsletterItem.Date = Convert.ToDateTime(_ListItem["Created"].ToString());
                    if (_ListItem["Title"] != null) { _NewsletterItem.Title = _ListItem["Title"].ToString(); }
                    if (_ListItem["Content"] != null) { _NewsletterItem.Content = _ListItem["Content"].ToString(); }
                    _NewsletterItems.Add(_NewsletterItem);
                }
            }
            //=====================================================================>>
            return _NewsletterItems;
        }
        public static Entities.Newsletter GetNewsletterItem(SPWeb _Web, int _Id)
        {
            Entities.Newsletter _NewsletterItem = new Entities.Newsletter();
            //=====================================================================>>
            if (IsListValid(_Web, Common.Helper._Const_NewsletterListName))
            {
                SPList _List = _Web.Lists[Common.Helper._Const_NewsletterListName];
                SPListItem _ListItem = _List.GetItemById(_Id);
                _NewsletterItem.Id = _ListItem.ID;
                _NewsletterItem.Date = Convert.ToDateTime(_ListItem["Created"].ToString());
                if (_ListItem["Title"] != null) { _NewsletterItem.Title = _ListItem["Title"].ToString(); }
                if (_ListItem["Content"] != null) { _NewsletterItem.Content = _ListItem["Content"].ToString(); }
            }
            return _NewsletterItem;
        }
        //==========================================================================================================================>>
        public static bool IsListValid(SPWeb _Web, string _ListName)
        {
            bool _IsValid = false;
            try
            {
                SPList _List = _Web.Lists[_ListName];
                _IsValid = true;
            }
            catch { }
            return _IsValid;
        }
        public static bool IsListValid(SPWeb _Web, Guid _Guid)
        {
            bool _IsValid = false;
            try
            {
                SPList _List = _Web.Lists[_Guid];
                _IsValid = true;
            }
            catch { }
            return _IsValid;
        }
        public static SPUser GetSPUser(SPListItem _ListItem, string _Key)
        {
            SPFieldUser _Field = _ListItem.Fields[_Key] as SPFieldUser;
            if (_Field != null && _ListItem[_Key] != null)
            {
                SPFieldUserValue fieldValue = _Field.GetFieldValue(_ListItem[_Key].ToString()) as SPFieldUserValue;
                if (fieldValue != null)
                {
                    return fieldValue.User;
                }
            }
            return null;
        }
        public static string GetWebProperty(SPWeb _Web, string _Key)
        {
            string _Value = "";
            if (_Web.AllProperties[_Key] != null)
            {
                _Value = _Web.AllProperties[_Key].ToString();
            }
            return _Value;
        }
        public static SPUser GetSPUserByListItem(SPListItem _ListItem, string _Key)
        {
            SPFieldUser _Field = _ListItem.Fields[_Key] as SPFieldUser;
            if (_Field != null)
            {
                SPFieldUserValue _FieldValue = _Field.GetFieldValue(_ListItem[_Key].ToString()) as SPFieldUserValue;
                if (_FieldValue != null)
                {
                    return _FieldValue.User;
                }
            }
            return null;
        }
    }
}
